export default {
  data() {
    return {
      programName: '', //金融方案名称
      upfloclick: true, //true展开收起false
      checkClick: false, //选中
      schemeTyps: '', //
      schemeTypsId: 0, //
      schemeTypexz: false, //方案类型
      numberid: 0, //首付比例

      dataTable: [{
        name: '12',
        lilv: '输入',
        shouxu: '输入2',
        id: 1,
        show: false,
      },
      {
        name: '24',
        lilv: '输入',
        shouxu: '输入2',
        id: 2,
        show: false,
      },
      {
        name: '36',
        lilv: '输入',
        shouxu: '输入2',
        id: 3,
        show: false,
      },
      {
        name: '48',
        lilv: '输入',
        shouxu: '输入2',
        id: 4,
        show: false,
      },
      {
        name: '60',
        lilv: '输入',
        shouxu: '输入2',
        id: 5,
        show: false,
      },
      ],
      list: [{
        id: 1,
        show: false,
      },
      {
        id: 2,
        show: false,
      },
      ],
      disabled: false,
      btnWidth: 180,
      show: false,
      options: [{
        text: '删除',
        style: {
          backgroundColor: '#EB5C02',
        },
      }, ],
      pageskulist: [{
        id: 0,
        show: false, //滑块删除显示
        iconShow: false, //icon显示隐藏
        Proportion: 0,
        children: [{
          Proportion: 0,
          LoanPeriods: 12,
          id: 1,
          InterestRate: null,
          Poundage: 0,
          Monthly: null,
        },
        {
          Proportion: 0,
          LoanPeriods: 24,
          id: 2,
          InterestRate: null,
          Poundage: 0,
          Monthly: null,
        },
        {
          Proportion: 0,
          LoanPeriods: 36,
          id: 3,
          InterestRate: null,
          Poundage: 0,
          Monthly: null,
        },
        {
          Proportion: 0,
          LoanPeriods: 48,
          id: 4,
          InterestRate: null,
          Poundage: 0,
          Monthly: null,
        },
        {
          Proportion: 0,
          LoanPeriods: 60,
          id: 5,
          InterestRate: null,
          Poundage: 0,
          Monthly: null,
        },
        ],
      }, ], //页面渲染选择使用------------------------------------------------商品规格sku列表
      PlanList: [], //最后提交的值
      moneynum: 0, //裸车价
      Monthly: '', //月供
      newsshow: false, //数字键盘
      value: '',
      index: null,
      index1: null,
      indexname: '',
      itemone: '',
      itemtwo: '',
    }
  },
  onLoad(type) {
    this.pduan = type.type
    if (this.pduan === '0') {
      this.schemeTyps = '低首付'
    } else {
      this.schemeTyps = '以租代售'
    }
    if (type.moneynum === 'null') {
      this.moneynum = 0
    } else {
      this.moneynum = Number(type.moneynum)
    }
  },
  methods: {
    keyclick(item, item1, index, index1, name) {
      this.newsshow = true
      this.index = index
      this.index1 = index1
      this.indexname = name
      this.itemone = item
      this.itemtwo = item1
    },
    // 按键被点击(点击退格键不会触发此事件)
    valChange(val) {
      // 将每次按键的值拼接到value变量中，注意+=写法
      var lengthshow = false

      if (this.indexname === 'InterestRate') {
        if (this.pageskulist[this.index].children[this.index1][this.indexname]) {
          if (this.pageskulist[this.index].children[this.index1][this.indexname].length < 3) {
            lengthshow = true
          }
        } else {
          lengthshow = true
        }
      } else {
        // 手续费
        if (this.pageskulist[this.index].children[this.index1][this.indexname]) {
          if (this.pageskulist[this.index].children[this.index1][this.indexname].length < 7) {
            lengthshow = true
          }
        } else {
          lengthshow = true
        }
      }
      if (lengthshow) {
        this.pageskulist[this.index].children[this.index1][this.indexname] = this.pageskulist[this.index].children[
          this.index1][this.indexname] + val + ''

        if (this.indexname === 'InterestRate') {
          this.GetCarCalculator(
            this.moneynum,
            this.itemone,
            Number(this.pageskulist[this.index].children[this.index1][this.indexname]),
            this.itemtwo
          )
        }
      }
    },
    // 退格键被点击
    backspace() {
      // 删除value的最后一个字符
      this.pageskulist[this.index].children[this.index1][this.indexname] = this.pageskulist[this.index].children[this
        .index1][this.indexname].substr(0, this.pageskulist[this.index].children[this.index1][this.indexname].length -
        1)

      if (this.indexname === 'InterestRate') {
        this.GetCarCalculator(
          this.moneynum,
          this.itemone,
          Number(this.pageskulist[this.index].children[this.index1][this.indexname]),
          this.itemtwo
        )
      }

      console.log(this.value)
    },
    //当键盘输入时input
    InputChildItem: function(e, item, item2, htype, index, index1) {
      // console.log(1, e) //输入值
      // console.log(2, item) //渲染表格的值
      // console.log(3, item2) //当前值
      // console.log(4, htype) //当前input
      setTimeout(() => {
        // 纯数字正则
        var reg = /^[0-9]*$/
        if (!reg.test(e.detail.value)) {
          this.$refs.uToast.show({
            title: '请输入整数',
            type: 'error',
            url: '/pages/user/index',
          })
          this.pageskulist[index].children[index1].InterestRate = null
          return false
        }
      }, 100)

      var olditem = this.PlanList.filter(ci => {
        //临时id = 渲染表格的id && 临时名称 = 当前名称   返回临时数据
        if (ci.carnameid === item.id && ci.LoanPeriods === item2.name) {
          console.log(1122, ci)
          return ci
        }
      })
      // console.log(123321, olditem)
      var owneritem = olditem.length > 0 ? olditem[0] : {}

      switch (htype) {
      case 'Guideprice':
        owneritem.InterestRate = Number(e.detail.value)
        this.GetCarCalculator(
          this.moneynum,
          item,
          owneritem.InterestRate,
          item2
        )
        break
      case 'Quote':
        owneritem.Poundage = Number(e.detail.value)

        break
      }

      if (olditem.length > 0) {
        owneritem.carnameid = item.id
      } else {
        owneritem.carnameid = item.id
        owneritem.LoanPeriods = item2.name
        owneritem.Proportion = item.id
        // this.PlanList.push(owneritem)
        // console.log('this.PlanList', this.PlanList)
      }
    },
    // 返回
    fanhui() {
      //图片地址
      uni.$emit('carImageS', {
        carImageS: this.carImageS,
      })
      uni.navigateBack()
      uni.$off('currentImgs')
    },
    //低首付
    lowDownPChange: function(e) {
      if (this.lowDownPayment) {
        this.fullAmount = false
      }
    },
    //点击展开收起
    upClick() {
      this.upfloclick = !this.upfloclick
    },
    // 新增金融
    newFinanSClick() {},
    //选择低首付以租代售
    sTypeClick(type) {
      this.schemeTypexz = false

      if (type === 0) {
        this.schemeTyps = '低首付'
      } else {
        this.schemeTyps = '以租代售'
      }
      this.schemeTypsId = type
    },

    //删除列表
    clickdelete(item, index) {
      this.pageskulist[index].children = []
      this.pageskulist[index].iconShow = true
      // var findex = this.PlanList.findIndex((item1, i) => item.id === item1.carnameid)
      // this.PlanList.splice(i, 1)

      let listbox = [] //暂存赋值
      this.PlanList.forEach((item1, i) => {
        if (item.id === item1.carnameid) {
          item1.carnameid = 10
        }
        if (item1.carnameid !== 10) {
          listbox.push(item1)
        }
      })
      this.PlanList = listbox

      this.$u.toast('成功删除')
    },
    // 如果打开一个的时候，不需要关闭其他，则无需实现本方法
    open(index) {
      // 先将正在被操作的swipeAction标记为打开状态，否则由于props的特性限制，
      // 原本为'false'，再次设置为'false'会无效
      this.pageskulist[index].show = true
      this.pageskulist.map((val, idx) => {
        if (index !== idx) this.pageskulist[idx].show = false
      })
      console.log(this.pageskulist)
    },
    //删除之后出现的icon,点击传入id
    iconxzengD(type) {
      this.pageskulist.forEach((item, i) => {
        if (i === type) {
          item.iconShow = false
          item.children = [{
            LoanPeriods: 12,
            id: 1,
            InterestRate: null,
            Poundage: 0,
            Monthly: null,
          },
          {
            LoanPeriods: 24,
            id: 2,
            InterestRate: null,
            Poundage: 0,
            Monthly: null,
          },
          {
            LoanPeriods: 36,
            id: 3,
            InterestRate: null,
            Poundage: 0,
            Monthly: null,
          },
          {
            LoanPeriods: 48,
            id: 4,
            InterestRate: null,
            Poundage: 0,
            Monthly: null,
          },
          {
            LoanPeriods: 60,
            id: 5,
            InterestRate: null,
            Poundage: 0,
            Monthly: null,
          },
          ]
        }
      })
      console.log('增加', this.pageskulist)
    },
    //点击icon新增
    iconxzeng(type) {
      if (this.pageskulist.length > 7) {
        this.$refs.uToast.show({
          title: '只能创建8组',
          type: 'warning',
          url: '/pages/user/index',
        })
        return
      }
      var palid
      this.pageskulist.forEach(item => {
        palid = item.Proportion
        palid = ++palid
      })
      // this.numberid++
      let datalist = {
        Proportion: palid,
        show: false,
        iconShow: false, //icon显示隐藏
        children: [{
          Proportion: palid,
          LoanPeriods: 12,
          InterestRate: null,
          Poundage: 0,
          Monthly: null,
        },
        {
          Proportion: palid,
          LoanPeriods: 24,
          InterestRate: null,
          Poundage: 0,
          Monthly: null,
        },
        {
          Proportion: palid,
          LoanPeriods: 36,
          InterestRate: null,
          Poundage: 0,
          Monthly: null,
        },
        {
          Proportion: palid,
          LoanPeriods: 48,
          InterestRate: null,
          Poundage: 0,
          Monthly: null,
        },
        {
          Proportion: palid,
          LoanPeriods: 60,
          InterestRate: null,
          Poundage: 0,
          Monthly: null,
        },
        ],
      }
      this.pageskulist = this.pageskulist.concat(datalist)
      console.log('点击icon添加', this.pageskulist)
    },
    // 输入框失去焦点时触发，event.detail = {value: value}
    blure(e, index, index1) {
      console.log('金融方案输完：', e.detail.value, index, index1)
      if (e.detail.value >= 1000) {
        this.pageskulist[index].children[index1].InterestRate = null
        this.pageskulist[index].children[index1].Monthly = null
        this.$refs.uToast.show({
          title: '贷款利率格式不正确',
          type: 'warning',
          url: '/pages/user/index',
        })
      }
    },
    //保存
    baocunClick() {
      let status = true
      this.pageskulist.forEach((item, i) => {
        item.children.forEach((arr, j) => {
          //如果利率大于等于1000，清空该利率
          if (Number(arr.InterestRate) >= 1000) {
            this.$refs.uToast.show({
              title: '贷款利率格式不正确2',
              type: 'warning',
              url: '/pages/user/index',
            })
            item.children[j].InterestRate = null
            item.children[j].Monthly = null
            status = false
            return false
          }
        })
      })
      //如果利率小于1000，调取接口
      if (status) {
        this.AddLoanPlan()
      }
    },
    //发布金融方案
    async AddLoanPlan() {
      if (this.schemeTyps === '低首付') {
        this.schemeTypsId = 0
      } else {
        this.schemeTypsId = 1
      }

      let newlist = []
      //把多个数组合并成一个，传给后台的格式就是一个数组。
      this.pageskulist.forEach(item => {
        item.children.forEach(item2 => {
          if (item2.InterestRate !== null) {
            if (item2.InterestRate) {
              item2.InterestRate = Number(item2.InterestRate)
              if (item2.Poundage === null) {
                item2.Poundage = 0
              }
              newlist.push(item2)
              console.log('newkistttttttt', newlist)
            }
          }
        })
      })
      this.PlanList = newlist
      if (this.PlanList.length === 0) {
        setTimeout(function() {
          uni.showToast({
            title: '请添加方案',
            duration: 1000,
            icon: 'none',
          })
        }, 100)
        return false
      }

      try {
        let data = {
          Name: this.programName, //金融方案名称
          LoanPlan_type: this.schemeTypsId, //数字类型  分类（0低首付，1以租代售）
          PlanList: this.PlanList,
          //   'Proportion': 0, //首付比例
          //   'LoanPeriods': 0, //贷款期数
          //   'InterestRate': 0, //贷款利率(如果不填则是-1)
          //   'Poundage': 0 //金融手续费(如果不填则是-1)
          Store_Id: this.$store.state.userInfoDetail.store.storeId, //店铺id
        }
        const res = await this.$api.carSource.AddLoanPlan(data)
        if (res.code === 200) {
          setTimeout(function() {
            uni.showToast({
              title: '新增成功',
              duration: 2000,
              icon: 'none',
            })
          }, 100)
          uni.navigateBack()
        }
      } catch (err) {
        console.log(err)
      }
    },
    //计算月供
    async GetCarCalculator(Price, downps, Coeff, dqitem) {
      //渲染表格的值  item2当前值
      try {
        let data = {
          salePrice: Price, //销售价格
          payment: downps.Proportion, //首付几成
          Coefficient: Coeff, //万元系数
        }
        const res = await this.$api.carSource.GetCarCalculator(data)
        this.Monthly = res.data
        this.pageskulist.forEach((item, i) => {
          if (i === downps.Proportion) {
            item.iconShow = false
            item.children.forEach((item1, j) => {
              if (item1.LoanPeriods === dqitem.LoanPeriods) {
                item1.Monthly = this.Monthly
              }
            })
          }
        })
        // console.log('pageskulist', this.pageskulist)
      } catch (err) {
        console.log(err)
      }
    },
  },
}
